import akshare as ak
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Kline

# df = ak.stock_zh_a_hist("600734", period='daily', start_date="20230101", end_date="20230630")
# print(df)
stock_data = ak.fund_etf_hist_em(symbol='512170', start_date='20230101', end_date='20240205')
# print(stock_data)

# k线数据
kline_data = []
for index, row in stock_data.iterrows():
  kline_data.append([row['开盘'], row['收盘'], row['最低'], row['最高']])

# 配置 Kline 图
kline = (
  Kline()
  .add_xaxis(xaxis_data=stock_data['日期'].to_list())
  .add_yaxis(series_name="日K线", y_axis=kline_data)
  .set_global_opts(
    xaxis_opts=opts.AxisOpts(is_scale=True),
    yaxis_opts=opts.AxisOpts(is_scale=True),
    title_opts=opts.TitleOpts(title="医疗ETF日k线"),
    datazoom_opts=[opts.DataZoomOpts()],
    toolbox_opts=opts.ToolboxOpts(
      feature={
        "dataZoom": {"yAxisIndex": "none"},
        "restore": {},
        "saveAsImage": {},
      }
    ),
  )
)

# 渲染图表
kline.render("kline_chart.html")